package jsyf.oa.dao.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.system.ShouldSignHolidayEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

@Repository
public class ShouldSignHolidayDao extends BaseDao {

    public ShouldSignHolidayDao() {
        super.iBaseEntity = new ShouldSignHolidayEntity();
    }

    public List<ShouldSignHolidayEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<ShouldSignHolidayEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            ShouldSignHolidayEntity entity = (ShouldSignHolidayEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, ShouldSignHolidayEntity.class);
    }

    public List<JSONObject> getShouldSignMonthList(String date, String fontType) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append("SHOULD_DATE AS holiday ");
        sb.append("FROM T_SYS_SHOULD_HOLIDAY ");
        sb.append("WHERE ");
        sb.append("left(SHOULD_DATE,7)='" + date + "' and FRONT_FLAG=" + fontType + " AND HOLIDAY_FLAG=1;");
        return queryForJsonList(sb.toString());
    }

    public void insertBatch(List<String> arrayDayList, String chooseDates, String frontType, String userId) {
        List<LinkedHashMap<String, Object>> shouldHolidayList = new ArrayList<LinkedHashMap<String, Object>>();
        LinkedHashMap<String, Object> shouldHolidayMap = null;
        List<String> chooseDateList = BaseJdbcDao.stringToList(chooseDates);
        for (int i = 0; i < arrayDayList.size(); i++) {
            String holidayFlag = "";
            shouldHolidayMap = new LinkedHashMap<>();
            String shouldDate = arrayDayList.get(i);
            shouldHolidayMap.put("ID", super.generateKey());
            shouldHolidayMap.put("FRONT_FLAG", frontType);
            shouldHolidayMap.put("SHOULD_DATE", shouldDate);
            shouldHolidayMap.put("CREATE_USER_ID", userId);
            shouldHolidayMap.put("CREATE_TIME", super.getCurrentTime());
            for (int j = 0; j < chooseDateList.size(); j++) {
                if (shouldDate.equals(chooseDateList.get(j))) {
                    holidayFlag = "1";
                }
            }
            if ("".equals(holidayFlag)) {
                holidayFlag = "0";
            }
            shouldHolidayMap.put("HOLIDAY_FLAG", holidayFlag);
            shouldHolidayMap.put("SHOULD_SIGN_IN", arrayDayList.size() - chooseDateList.size());
            shouldHolidayList.add(shouldHolidayMap);
        }
        super.insertBatch("t_sys_should_holiday", shouldHolidayList);
    }
}

